<?php

namespace app\controller\Web;

use app\service\Admin\CommonService;
use app\util\ResponseTrait;
use Exception;
use support\Request;
use Throwable;

use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("公共接口")
 */
class IndexController
{
    use ResponseTrait;

    /**
     * @Apidoc\Url("/web/uploadPic")
     * @Apidoc\Title("图片上传")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("file",type="file",require=true,default="",desc="文件（使用body中form-data方式上传）")
     * @Apidoc\Returned("id", type="string",desc="文件ID")
     * @Apidoc\Returned("src", type="string",desc="文件远程访问路径（带http）")
     * @Apidoc\Returned("fileName", type="string",desc="文件名称")
     * @Apidoc\Returned("path", type="string",desc="文件存储路径")
     */
    public function uploadPic(Request $request)
    {
        $file = $request->file('file');

        try {
            if ($file == null) {
                throw new Exception('未找到上传文件');
            }
            $data = CommonService::uploadFile($file, ['jpg', 'jpeg', 'png', 'mbp', 'gif']);

            return $this->success($data, '上传成功');
        } catch (Throwable $e) {
            return $this->fail($e);
        }
    }

    /**
     * @Apidoc\Url("/web/uploadFile")
     * @Apidoc\Title("文件上传")
     * @Apidoc\Method("POST")
     * @Apidoc\Param("file",type="file",require=true,default="",desc="文件（使用body中form-data方式上传）")
     * @Apidoc\Returned("id", type="string",desc="文件ID")
     * @Apidoc\Returned("src", type="string",desc="文件访问路径（带http）")
     * @Apidoc\Returned("fileName", type="string",desc="文件名称")
     * @Apidoc\Returned("path", type="string",desc="文件路径")
     */
    public function uploadFile(Request $request)
    {
        $file = $request->file('file');

        try {
            if ($file == null) {
                throw new Exception('未找到上传文件');
            }
            $data = CommonService::uploadFile($file, ['xls', 'xlsx', 'pdf', 'xls', 'xlsx', 'doc', 'docx', 'ppt', 'zip', 'pptx', 'mp4', 'flv'], 'file');

            return $this->success($data, '上传成功');
        } catch (Throwable $e) {
            return $this->fail($e);
        }
    }

}
